import qs from 'qs';
import { request } from '../request';

/** 添加好友（新版：userId, friendKeyword, tag） */
export function addFriend(data: { userId: number; friendKeyword: string; tag?: string }) {
  return request({
    url: '/api/friend/add.do',
    method: 'post',
    data
  });
}

/** 添加好友（真实接口：userId, friendId, tag） */
export function addFriendReal(data: { userId: number; friendId: number; tag: string }) {
  return request({
    url: '/api/friend/addFriend.do',
    method: 'post',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    data: qs.stringify(data)
  });
}

/** 获取好友列表 */
export function fetchFriendList(userId: number) {
  return request({
    url: '/api/friend/user.do',
    method: 'get',
    params: { userId }
  });
}

/** 获取好友详情 */
export function fetchFriendInfo(userId: number, friendId: number) {
  return request({
    url: '/api/friend/info.do',
    method: 'get',
    params: { userId, friendId }
  });
}

/** 删除好友 */
export function deleteFriend(userId: number, friendId: number) {
  const payload = { userId, friendId };
  const config = {
    url: '/api/friend/delete.do',
    method: 'post',
    data: payload
  };
  console.log('[deleteFriend] 请求参数:', payload);
  console.log('[deleteFriend] request配置:', config);
  return request(config);
}

/** 搜索好友 */
export function searchFriendApi(friendKeyword: string) {
  return request({
    url: '/api/friend/searchFriend.do',
    method: 'get',
    params: { friendKeyword }
  });
}

/** 搜索好友（带游戏库） */
export function searchFriendWithGamesApi(friendKeyword: string) {
  return request({
    url: '/api/friend/searchFriendWithGames.do',
    method: 'get',
    params: { friendKeyword }
  });
}

/** 获取收到的好友申请列表 */
export function getFriendRequestList(userId: number) {
  return request({
    url: '/api/friend/requestList.do',
    method: 'get',
    params: { userId }
  });
}

/** 同意好友申请 */
export function acceptFriendRequest(data: { userId: number; friendId: number }) {
  return request({
    url: '/api/friend/accept.do',
    method: 'post',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    data: qs.stringify(data)
  });
}

/** 拒绝好友申请 */
export function rejectFriendRequest(data: { userId: number; friendId: number }) {
  return request({
    url: '/api/friend/reject.do',
    method: 'post',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    data: qs.stringify(data)
  });
}

/** 查询所有PENDING状态的好友（双向） */
export function getAllPendingFriends(params: {
  userId: number;
  pageNum: number;
  pageSize: number;
  name?: string;
  email?: string;
}) {
  return request({
    url: '/api/friend/allWithPendingPage.do',
    method: 'get',
    params
  });
}
